package com.sh.test_one.adapater;

import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;



import com.sh.test_one.R;
import com.sh.test_one.bean.SubscriptionOption;
import com.sh.test_one.listener.OnItemClickListener;

import java.util.List;

public class SubscriptionAdapter extends RecyclerView.Adapter<SubscriptionAdapter.SubscriptionViewHolder> {
    private List<SubscriptionOption> subscriptionOptions;
    private Context context;

    private OnItemClickListener listener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public SubscriptionAdapter(Context context, List<SubscriptionOption> subscriptionOptions) {
        this.context = context;
        this.subscriptionOptions = subscriptionOptions;
    }

    @NonNull
    @Override
    public SubscriptionViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_price, parent, false);
        return new SubscriptionViewHolder(view);
    }



    @Override
    public void onBindViewHolder(@NonNull SubscriptionViewHolder holder, int position) {
        SubscriptionOption option = subscriptionOptions.get(position);
        holder.textViewDuration.setText(option.getDuration());
        holder.textViewPrice.setText(String.format("¥%.2f", option.getDailyPrice()));
        SpannableString spannableString = new SpannableString(String.format("¥%.2f", option.getPrice()));
        spannableString.setSpan(new StrikethroughSpan(), 0, spannableString.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        holder.textViewDailyPrice.setText(spannableString);

        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(v, position, option); // 传递更多参数
            }
        });
    }

    @Override
    public int getItemCount() {
        return subscriptionOptions.size();
    }

    public  class SubscriptionViewHolder extends RecyclerView.ViewHolder {
        TextView textViewDuration;
        TextView textViewPrice;
        TextView textViewDailyPrice;

        public SubscriptionViewHolder(@NonNull View itemView) {
            super(itemView);
            textViewDuration = itemView.findViewById(R.id.text_view_duration);
            textViewPrice = itemView.findViewById(R.id.text_view_price);
            textViewDailyPrice = itemView.findViewById(R.id.text_view_daily_price);
        }
    }
}